<?php
namespace app\staff\controller;

use think\Request;
use think\Db;
use think\Session;
use think\Controller;
use think\Config;
use Godok\Org\Auth;
use think\Log;

class Purchase extends Controller
{
	//采购详情列表
	function index(){
		$purchase_id = Request()->param('purchase_id');
		$time = Db::name('purchase')->where('purchase_id',$purchase_id)->value('create_time');
		$purchase['list'] = Db::table('sdzx_purchase_goods')->alias('pg')
		->field('pg.pg_id,pg.goods_id,pg.specval_id,pg.specval_name,pg.spec_name,pg.goods_name,pg.picture,pg.num,pg.real_num,pg.last_price,pg.price,pg.cur_price,pg.purchase_id,pg.supplier_id,pg.brand_id,pg.brand_name,s.charge_phone')
		->join('sdzx_supplier s','pg.supplier_id=s.supplier_id','left')
		->where(['pg.purchase_id'=>$purchase_id,'pg.status'=>0])
		->select();
		
		foreach ($purchase['list'] as $key => $value) {
			$purchase['list'][$key]['order'] = Db::table('sdzx_order_goods')->alias('og')
			->field('o.order_id,o.receiver_name,o.receiver_mobile,og.num,sp.shop_name')
			->join('sdzx_order o','o.order_id=og.order_id')
			->join('sdzx_shop sp','o.user_id=sp.user_id','left')
			->where(['og.goods_id'=>$value['goods_id'],'o.order_status'=>30])
			->select();
		}
		$purchase['time'] = date('Y-m-d',strtotime($time));
		$this->success('success','',$purchase);
	}

	//取消采购
	function del(){
		$pg_id = Request()->param('pg_id');
		$cancel_num = Request()->param('cancel_num');
		$remarks = Request()->param('remarks');
		$data = [
			'remark'		=> $remarks,
			'cancel_num'	=> $cancel_num,
			'cancel_status'	=> 1,
			'cancel_time'	=> date('Y-m-d H:i:s')
		];
		$res = Db::name('purchase_goods')->where('pg_id',$pg_id)->update($data);
		$re = Db::name('purchase_goods')->where('pg_id',$pg_id)->setDec('num',$cancel_num);
		if($res || $re){
			$this->success('取消成功');
		}else{
			$this->error('取消失败');
		}
	}

	//全部供应商
	function supplier(){
		$list = Db::name('supplier')->where('supplier_status',1)->select();
		if($list){
			$this->success('success','',$list);
		}else{
			$this->error('error');
		}
	}

	/* 开始采购展示页 */
	function purchase(){
		$pg_id = Request()->param('pg_id');
		$purchase = Db::table('sdzx_purchase_goods')->alias('pg')
		->field('pg.brand_id,pg.brand_name,pg.goods_name,pg.spec_name,pg.num,pg.real_num,pg.price,pg.cur_price,s.supplier_id,s.supplier_name,pg.pay_status,pg.apply_price')
		->join('sdzx_supplier s','s.supplier_id=pg.supplier_id','left')
		->where('pg.pg_id',$pg_id)
		->find();
		if($purchase){
			$this->success('success','',$purchase);
		}else{
			$this->error('error');
		}
	}

	/* 提交/修改 采购 */
	function subPurchase(){
		$real_num = Request()->param('real_num');					//实际采购数量
		$cur_price = Request()->param('cur_price');					//本次采购价格
		$supplier_id = Request()->param('supplier_id');				//供应商id
		$pay_status = Request()->param('pay_status');				//支付状态	0:未付 1：已付
		$apply_price = Request()->param('apply_price');				//改价申请	0：不申请 1：申请 2：已改价 -1：已忽略
		$pg_id = Request()->param('pg_id');
		$data = [
			'real_num'	=> $real_num,
			'cur_price'	=> $cur_price,
			'supplier_id'	=> $supplier_id,
			'pay_status'	=> $pay_status,
			'apply_price'	=> $apply_price
		];
		$edit = Db::name('purchase_goods')->where('pg_id',$pg_id)->update($data);
		if($edit!==false){
			$this->success('修改成功');
		}else{
			$this->error('修改失败');
		}
	}
}